<?php
//财务管理
namespace App\Http\Controllers\App;
use App\Http\Controllers\App\BaseController as Controller;
use App\Models\Order;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class ManagementFinance extends Controller
{
     //财务展示
    public function showFinance(Request $request) {
        try{
            $rules = [
                'shop_id'=>'required|integer|exists:shops,id',
                'money_type'=>'integer|in:0,1',
                'type'=>'integer|in:0,1,2,3',
                'start'=>'string',
                'end'=>'string'
            ];
            $message = [
                'shop_id.*' => '商家信息有误',
                'type.*'=>'所选类型有误',
                'money_type.*'=>'请选择正确的到账情况',
                'start.*'=>'请选择起始时间',
                'end.*'=>'请选择终止时间',
            ];
            $validator = Validator::make($request->all(), $rules,$message);
            if ($validator->fails()) {
                return show(2003,$validator->errors()->first());
            }
            $shop_id = $request->shop_id;
            $money_type = $request->money_type;
            $type = $request->type;
            $perpage = 15;
            $all_order_status = ['2','3','4','5'];  // 所有的可用订单状态
            $all_order_type = ['0','1','2','3'];    //    订单类型(0外卖,1预定 ,2排队 ,3扫码 ,4直接付款 ,5充值)
            $in_order_status = ['4','5'];  //到账状态
            $notin_order_status = ['2','3'];  //未到账状态
            $start = $request->start;
            $end = $request->end;
            if (!isset($end)){
                $end = date("Y-m-d H:i:s");
            }
            if (isset($money_type)){
                if ($money_type == 1){    //到账
                    if (isset($type)){      // 根据外卖，预定，排队，扫码 分别处理
                        if (isset($start)){     // 如果有选择时间时
                            $orders_sum = Order::where('shop_id',$shop_id)
                                ->where('order_type',$type)
                                ->whereBetween('created_at',[$start,$end])
                                ->whereIn('order_status',$in_order_status)
                                ->orderBy('created_at','desc')
                                ->sum('order_price');

                            $orders = Order::with(['users'=>function($query){
                                $query->select('id','name','mobile');
                            }])->select('id','order_number','user_id','order_price','order_amount','order_status','order_type','created_at')
                                ->where('shop_id',$shop_id)
                                ->where('order_type',$type)
                                ->whereBetween('created_at',[$start,$end])
                                ->whereIn('order_status',$in_order_status)
                                ->orderBy('created_at','desc')
                                ->paginate($perpage);
                        }else{
                            $orders_sum = Order::where('shop_id',$shop_id)
                                ->where('order_type',$type)
                                ->whereIn('order_status',$in_order_status)
                                ->orderBy('created_at','desc')
                                ->sum('order_price');

                            $orders = Order::with(['users'=>function($query){
                                $query->select('id','name','mobile');
                            }])->select('id','order_number','user_id','order_price','order_amount','order_status','order_type','created_at')
                                ->where('shop_id',$shop_id)
                                ->where('order_type',$type)
                                ->whereIn('order_status',$in_order_status)
                                ->orderBy('created_at','desc')
                                ->paginate($perpage);
                        }

                    }else{
                        if (isset($start)) {     // 如果有选择时间时
                            $orders_sum = Order::where('shop_id',$shop_id)
                                ->whereBetween('created_at',[$start,$end])
                                ->whereIn('order_type',$all_order_type)
                                ->whereIn('order_status',$in_order_status)
                                ->orderBy('created_at','desc')
                                ->sum('order_price');

                            $orders = Order::with(['users'=>function($query){
                                $query->select('id','name','mobile');
                            }])->select('id','order_number','user_id','order_price','order_amount','order_status','order_type','created_at')
                                ->where('shop_id',$shop_id)
                                ->whereBetween('created_at',[$start,$end])
                                ->whereIn('order_type',$all_order_type)
                                ->whereIn('order_status',$in_order_status)
                                ->orderBy('created_at','desc')
                                ->paginate($perpage);
                        }else{
                            $orders_sum = Order::where('shop_id',$shop_id)
                                ->whereIn('order_type',$all_order_type)
                                ->whereIn('order_status',$in_order_status)
                                ->orderBy('created_at','desc')
                                ->sum('order_price');

                            $orders = Order::with(['users'=>function($query){
                                $query->select('id','name','mobile');
                            }])->select('id','order_number','user_id','order_price','order_amount','order_status','order_type','created_at')
                                ->where('shop_id',$shop_id)
                                ->whereIn('order_type',$all_order_type)
                                ->whereIn('order_status',$in_order_status)
                                ->orderBy('created_at','desc')
                                ->paginate($perpage);
                        }

                    }
                }else{      // 未到账
                    if (isset($start)){
                        $orders_sum = Order::where('shop_id',$shop_id)
                            ->whereBetween('created_at',[$start,$end])
                            ->whereIn('order_type',$all_order_type)
                            ->whereIn('order_status',$notin_order_status)
                            ->orderBy('created_at','desc')
                            ->sum('order_price');

                        $orders = Order::with(['users'=>function($query){
                            $query->select('id','name','mobile');
                        }])->select('id','order_number','user_id','order_price','order_amount','order_status','order_type','created_at')
                            ->where('shop_id',$shop_id)
                            ->whereBetween('created_at',[$start,$end])
                            ->whereIn('order_type',$all_order_type)
                            ->whereIn('order_status',$notin_order_status)
                            ->orderBy('created_at','desc')
                            ->paginate($perpage);

                    }else{
                        $orders_sum = Order::where('shop_id',$shop_id)
                            ->whereIn('order_type',$all_order_type)
                            ->whereIn('order_status',$notin_order_status)
                            ->orderBy('created_at','desc')
                            ->sum('order_price');

                        $orders = Order::with(['users'=>function($query){
                            $query->select('id','name','mobile');
                        }])->select('id','order_number','user_id','order_price','order_amount','order_status','order_type','created_at')
                            ->where('shop_id',$shop_id)
                            ->whereIn('order_type',$all_order_type)
                            ->whereIn('order_status',$notin_order_status)
                            ->orderBy('created_at','desc')
                            ->paginate($perpage);
                    }

                }

            }else{
                if (isset($type)){
                    if (isset($start)){
                        $orders_sum = Order::where('shop_id',$shop_id)
                            ->where('order_type',$type)
                            ->whereBetween('created_at',[$start,$end])
                            ->whereIn('order_status',$all_order_status)
                            ->orderBy('created_at','desc')
                            ->sum('order_price');
                        $orders = Order::with(['users'=>function($query){
                            $query->select('id','name','mobile');
                        }])->select('id','order_number','user_id','order_price','order_amount','order_status','order_type','created_at')
                            ->where('shop_id',$shop_id)
                            ->where('order_type',$type)
                            ->whereBetween('created_at',[$start,$end])
                            ->whereIn('order_status',$all_order_status)
                            ->orderBy('created_at','desc')
                            ->paginate($perpage);
                    }else{
                        $orders_sum = Order::where('shop_id',$shop_id)
                            ->where('order_type',$type)
                            ->whereIn('order_status',$all_order_status)
                            ->orderBy('created_at','desc')
                            ->sum('order_price');
                        $orders = Order::with(['users'=>function($query){
                            $query->select('id','name','mobile');
                        }])->select('id','order_number','user_id','order_price','order_amount','order_status','order_type','created_at')
                            ->where('shop_id',$shop_id)
                            ->where('order_type',$type)
                            ->whereIn('order_status',$all_order_status)
                            ->orderBy('created_at','desc')
                            ->paginate($perpage);
                    }

                }else{
                    if (isset($start)){
                        $orders_sum = Order::where('shop_id',$shop_id)
                            ->whereBetween('created_at',[$start,$end])
                            ->whereIn('order_type',$all_order_type)
                            ->whereIn('order_status',$all_order_status)
                            ->orderBy('created_at','desc')
                            ->sum('order_price');
                        $orders = Order::with(['users'=>function($query){
                            $query->select('id','name','mobile');
                        }])->select('id','order_number','user_id','order_price','order_amount','order_status','order_type','created_at')
                            ->where('shop_id',$shop_id)
                            ->whereBetween('created_at',[$start,$end])
                            ->whereIn('order_type',$all_order_type)
                            ->whereIn('order_status',$all_order_status)
                            ->orderBy('created_at','desc')
                            ->paginate($perpage);
                    }else{
                        $orders_sum = Order::where('shop_id',$shop_id)
                            ->whereIn('order_type',$all_order_type)
                            ->whereIn('order_status',$all_order_status)
                            ->orderBy('created_at','desc')
                            ->sum('order_price');
                        $orders = Order::with(['users'=>function($query){
                            $query->select('id','name','mobile');
                        }])->select('id','order_number','user_id','order_price','order_amount','order_status','order_type','created_at')
                            ->where('shop_id',$shop_id)
                            ->whereIn('order_type',$all_order_type)
                            ->whereIn('order_status',$all_order_status)
                            ->orderBy('created_at','desc')
                            ->paginate($perpage);
                    }

                }

            }
            if ($orders->isEmpty()){
                return show(201,'暂无订单信息',['orders'=>$orders,'sum'=>$orders_sum]);
            }
            return show(200,'获取成功',['orders'=>$orders,'sum'=>$orders_sum]);
        }catch (\Exception $e){
            return show(201,'暂无订单信息',['orders'=>$orders,'sum'=>$orders_sum]);
        }

    }
}
